#include "ResourceManager.h"

bool						MenuInit=false;
AudioDevicePtr				ResourceManager::audiereDevice;
OutputStreamPtr				ResourceManager::sound;
OutputStreamPtr				ResourceManager::sSound[SOUNDS];
OutputStreamPtr				ResourceManager::sSong[SONGS];
video::IVideoDriver*		ResourceManager::driver;
scene::ISceneManager*		ResourceManager::smgr;
scene::ITriangleSelector*	ResourceManager::selector;
//char**						ResourceManager::sModel;
//char**						ResourceManager::sModelLoaded;
//char**						ResourceManager::sTexture;
//int							ResourceManager::nModelIndex;
ResourceManager::Modelo		ResourceManager::Models[MAXMODEL];
int							ResourceManager::iLastModel;
cGUI						ResourceManager::Gui;
cMenu						ResourceManager::Menu;

video::ITexture*			ResourceManager::tNumbers;

scene::IAnimatedMeshSceneNode* ResourceManager::mModelLoaded;

void ResourceManager::DrawNumbers(int n, core::position2d<s32> s)
{


	rect<s32> r;
	r.UpperLeftCorner.X=0+(n%5)*20;
	r.UpperLeftCorner.Y=0+(n/5)*30;
	r.LowerRightCorner.X=19+(n%5)*20;
	r.LowerRightCorner.Y=29+(n/5)*30;

	
	Global::driver->draw2DImage(tNumbers, core::position2d<s32>(s.X,s.Y),
				r, 0, video::SColor(255,255,255,255), true);

}

void ResourceManager::ParseNumber(int n, core::position2d<s32> s)
{

	int aux = n;
	int digitos =0;
	while (aux>0) // ACA SACO CANTIDAD DE DIGITOS DE N
	{
		digitos++;
		aux/=10;
	}

	aux=digitos/2; // ACA AGARRO SOLO LA MITAD, PARA "CENTRAR" EL TEXTO

	do
	{
		ResourceManager::DrawNumbers(n%10,position2d<s32> (s.X+(aux*20),s.Y));
		aux--;
		n/=10;
	}
	while (aux>((digitos/2)-digitos)); // ACA IMPRIMO CARACTERES HASTA QUE LLEGUE A LA CANTIDAD DE DIGITOS
									   // SERIA LO MISMO QUE HACER UN CONTADOR QUE VAYA DE 0 A digitos
	
}

void ResourceManager::InitGui()
{
	Global::device->getGUIEnvironment()->grab();
	
	video::E_DRIVER_TYPE driverType;
	driverType = video::EDT_DIRECT3D8;

	Gui.SetDriverType(driverType);
	Gui.SetDevice(Global::device);
	Gui.SetUsePercentage(true);
	Gui.Init();
	Gui.SetHealth(100);
	Gui.SetMana(20);
	Global::device->setWindowCaption(L"Void's Wizard");
	Gui.SetUsePercentage(true);	
}

void ResourceManager::DrawEnv()
{
	Global::deviceMenu->getGUIEnvironment()->drawAll();
}

bool ResourceManager::InitMenu()
{
//	Global::deviceMenu->getGUIEnvironment()->grab();
	Global::deviceMenu->getCursorControl()->setVisible(true);
	if(!MenuInit)
	{
		Menu.AddMenuOption(L"Play");
		Menu.AddMenuOption(L"Credits", eMOT_CREDITOPTION);
		Menu.AddMenuOption(L"Exit", eMOT_QUITOPTION);
		MenuInit=true;
	}
	Menu.AddBackground("../media/fondo.bmp");
	Menu.Init();
	return true;
}

void ResourceManager::ShowGame()
{
	Global::driver->endScene();

//	Global::device->getGUIEnvironment()->drop();

	Global::device->closeDevice();

	Global::driver->beginScene(true, true, video::SColor(255, 113, 113, 133));

//	Global::device->getGUIEnvironment()->grab();
/*	video::E_DRIVER_TYPE driverType;
	driverType = video::EDT_DIRECT3D8;

	Gui.SetDriverType(driverType);
	Gui.SetDevice(Global::device);
	Gui.SetUsePercentage(true);
	Gui.SetHealth(50);
	Gui.SetMana(20);
	Gui.Init();
	Global::device->setWindowCaption(L"Void's Wizard");
	Gui.SetUsePercentage(true);	
*/

	ResourceManager::InitGui();
}

void ResourceManager::PlaySound(int sound,float volume)
{
	sSound[sound]->setRepeat(false);
	sSound[sound]->setVolume(volume); // 50% volume
	sSound[sound]->play();	
}

void ResourceManager::PlaySound(int sound)
{
	sSound[sound]->setRepeat(false);
	sSound[sound]->play();	
}

void ResourceManager::PlayMusic(int	song, float volume)
{
	sSong[song]->setRepeat(true);
	sSong[song]->setVolume(volume); // 50% volume
	sSong[song]->play();
}

void ResourceManager::PlayMusic(int	song)
{
	sSong[song]->setRepeat(true);
	sSong[song]->play();
}

void ResourceManager::StartAudio()
{
	audiereDevice = OpenDevice();
	if (!audiereDevice) 
		return;

	sSound[SOUND::Ball] = OpenSound(audiereDevice.get(), "../media/fire.wav", true);
	sSound[SOUND::Impact] = OpenSound(audiereDevice.get(), "../media/lg_fire.wav", true);
	sSong[MUSIC::ColdPlay] = OpenSound(audiereDevice.get(), "../media/cold1.mp3", true);
}

void ResourceManager::CreateCube(const irr::c8 *textureFile)
{
	//creacion del nodo/entity
	scene::ISceneNode* node = smgr->addCubeSceneNode();
	//se le da la textura
	video::ITexture* tex = driver->getTexture(textureFile);

	if (tex)
		node->setMaterialTexture(0,tex);

	node->setMaterialFlag(video::EMF_LIGHTING, false);
	
	node->setPosition(core::vector3df(0,0,30));
}

scene::IAnimatedMeshSceneNode* ResourceManager::CreateEntity(const irr::c8 *meshFile, const irr::c8 *textureFile)
{
	int i = 0;
	bool search = false;
	while((i<iLastModel) && (!search))
	{
		if (strcmp(meshFile, Models[i].sModel) == 0)
			search=true;
		else i++;
	}

	if(search)
		return CreateEntity(i);
	else
	{
		Models[iLastModel].mModel = smgr->addAnimatedMeshSceneNode(smgr->getMesh(meshFile));
		Models[iLastModel].tTexture = Global::driver->getTexture(textureFile);
		strcpy(Models[iLastModel].sModel, meshFile);
		return CreateEntity(iLastModel++); // LOAD CURRENT AND ADD ONE
	}
}

scene::IAnimatedMeshSceneNode* ResourceManager::CreateEntity(int meshIndex)
{
	cout << "Mesh index:" << meshIndex << endl;
	scene::IAnimatedMeshSceneNode *mesh;
	mesh = Models[meshIndex].mModel;
	mesh->setMaterialFlag(video::EMF_LIGHTING, false);
	mesh->setFrameLoop(320, 367);
	mesh->setAnimationSpeed(80);
	mesh->setMaterialTexture(0, Models[meshIndex].tTexture);
	return mesh;
}

void ResourceManager::LoadMap()
{
	Global::device->getFileSystem()->addZipFileArchive("../../media/map-20kdm2.pk3");
	scene::IAnimatedMesh* mesh = smgr->getMesh("20kdm2.bsp");
	scene::ISceneNode* node = 0;

	selector = smgr->createOctTreeTriangleSelector(mesh->getMesh(0), node);

	if (mesh)
		node = smgr->addOctTreeSceneNode(mesh->getMesh(0));
	
	if (node)
		node->setPosition(core::vector3df(-1300,-144,-1249));

}
void ResourceManager::Load3dsModel(const c8* filename,const c8* texturename)
{
/*	scene::IAnimatedMesh* mesh = smgr->getMesh(filename);
	scene::ISceneNode* node = 0;

	node = smgr->addAnimatedMeshSceneNode(mesh);
	if (texturename)
		node->setMaterialTexture(0,	driver->getTexture(texturename));
//	node->setRotation(core::vector3df(-90,0,0));
	node->setPosition(core::vector3df(0,-40,20));
	node->setScale(core::vector3df(0.5f,0.5f,0.5f));

	node->setMaterialFlag(video::EMF_LIGHTING, false);	*/
}

video::ITexture* ResourceManager::LoadTexture(const c8* filename)
{
	video::ITexture* tex = driver->getTexture(filename);

	if (tex){

		return tex;
	}else
		return 0;
}

bool ResourceManager::SetTextureNode(scene::ISceneNode* node,const c8* filename)
{
	video::ITexture* tex = driver->getTexture(filename);

	if (tex)
	{
		node->setMaterialTexture(0,tex);
		return true;
	}
	else return false;
}

bool ResourceManager::SetCubeForm(scene::ISceneNode* node)
{
/*	scene::ISceneNode* n = node;

	if (n){
		n = smgr->addCubeSceneNode();
		return true;
	}else
		return false;
*/		
	node = smgr->addSphereSceneNode();
	return 0;
}


scene::IAnimatedMeshSceneNode* ResourceManager::LoadAnimatedModel(const c8 *filename)
{
	scene::IAnimatedMeshSceneNode* m = smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/sydney.md2"));

	if (m)
		return m;
	else
		return 0;
}

void ResourceManager::SetStraightAnimator(scene::ISceneNode* node)
{
	scene::ISceneNodeAnimator* anim =smgr->createFlyStraightAnimator(core::vector3df(100,0,60),
		core::vector3df(-100,0,60), 2500, true);
	node->addAnimator(anim);
	anim->drop();

}